package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.customerstatement.pojo.dto.StatementCustomerAddDTO;
import com.xbongbong.pro.customerstatement.pojo.dto.StatementSummaryDetailDTO;
import com.xbongbong.pro.customerstatement.pojo.dto.StatementSummaryOperateDTO;
import com.xbongbong.pro.customerstatement.pojo.vo.StatementCustomerAddVO;
import com.xbongbong.pro.customerstatement.pojo.vo.StatementSummaryDetailVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.CustomerStatementStatisticService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Description 对账单列表相关
 * @author: shian
 * @date : 2021/1/4
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/statementSummary")
public class StatementSummaryController {
    @Resource
    private CustomerStatementStatisticService customerStatementStatisticService;

    /**
     * 添加到对账单列表
     * @param statementCustomerAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String generate(@RequestBody @Valid StatementCustomerAddDTO statementCustomerAddDTO, BindingResult br) {
        XbbResponse<StatementCustomerAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StatementCustomerAddVO statementCustomerAddVO = customerStatementStatisticService.generate(statementCustomerAddDTO);
                response = new XbbResponse<>(statementCustomerAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 对账单详细
     * @param detailDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid StatementSummaryDetailDTO detailDTO, BindingResult br) {
        XbbResponse<StatementSummaryDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                StatementSummaryDetailVO detailVO = customerStatementStatisticService.detail(detailDTO);
                response = new XbbResponse<>(detailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 对账单操作
     * @param operateDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/operate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String operate(@RequestBody @Valid StatementSummaryOperateDTO operateDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = customerStatementStatisticService.operate(operateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
